package net.shizhibao.www.mylibrary;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;

import net.shizhibao.www.mylibrary.view.MyWebViewClient;
import net.shizhibao.www.mylibrary.view.X5WebView;

import androidx.appcompat.app.AppCompatActivity;

public abstract class WebViewActivity extends AppCompatActivity {
    public MyWebViewClient x5WebViewClient;
    public Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        try {
            if (Integer.parseInt(android.os.Build.VERSION.SDK) >= 11) {
                getWindow()
                        .setFlags(
                                android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                                android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
            }
        } catch (Exception e) {
        }
        setContentView(getLayoutResID());
        x5WebViewClient = findViewById(getViewId());
        initView();
        initEvent();
        initData();
    }

    protected abstract int getLayoutResID();

    protected abstract  int getViewId();
    protected abstract void initView();
    public boolean isNotNull(){
        if (x5WebViewClient!=null&&x5WebViewClient.getWebView()!=null){
            return true;
        }
        return false;
    }
    public void onChangeTitle(String title){}
    public void pageFinish(){

    }
    protected  void initEvent(){
        x5WebViewClient.setOnPageListener(new X5WebView.OnPageListener() {


            @Override
            public void onPageFinish() {
                pageFinish();
            }

            @Override
            public void onTitleChange(String title) {
                onChangeTitle(title);
            }
        });
        if (isNotNull()&&
                x5WebViewClient.getWebView().getX5WebViewExtension()!=null)
        {
            Bundle data = new Bundle();
            data.putBoolean("standardFullScreen", false);
            //true表示标准全屏，false表示X5全屏；不设置默认false，
            data.putBoolean("supportLiteWnd", true);
            //false：关闭小窗；true：开启小窗；不设置默认true，
            data.putInt("DefaultVideoScreen", 1);
            //1：以页面内开始播放，2：以全屏开始播放；不设置默认：1
            x5WebViewClient.getWebView().getX5WebViewExtension().invokeMiscMethod("setVideoParams", data);

        }
        doSomeThing();

    };
    public abstract   void initData();
    public void loadUrl(String url){

        if (isNotNull()){
            x5WebViewClient.loadUrl(url);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (isNotNull()) {
            x5WebViewClient.getWebView().destroy();
        }

    }

    @Override
    public void onBackPressed() {
        if (!canBack()) {
            super.onBackPressed();
        }

    }
    public boolean canBack(){
        return canBack(true);
    }
    public boolean canBack(boolean back){
        if (isNotNull() && x5WebViewClient.getWebView().canGoBack()) {
            if (back){
                x5WebViewClient.getWebView().goBack();
            }
            return true;
        }
        return false;
    }

    public void doSomeThing(){

    }


}
